package com.man.controller;

import com.man.pojo.User;
import com.man.service.LoginService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @CallsName: LoginController
 */

@Controller
@Log4j2
@Validated
@CrossOrigin//跨域
public class LoginController {
    @Autowired
    private LoginService loginService;
    ModelAndView modelAndView = new ModelAndView();

    /*登录*/
    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public ModelAndView login(String username, String password, Model model, HttpServletRequest request) {
        User user = loginService.getByUser(username);
        if (user == null) {
            modelAndView.setViewName("login");
            return modelAndView;
//            return Result.error("用户不存在");
        }
        if (!user.getPassword().equals(password)) {
            modelAndView.setViewName("login");
            return modelAndView;
//            return Result.error("密码错误");
        }
        modelAndView.setViewName("redirect:/index");
        List<User> all = loginService.getAll();
        request.getSession().setAttribute("users", user);
        request.getSession().setAttribute("user", all);
        return modelAndView;
    }

    /*----------------------------*/
    /*首页*/
    @GetMapping("/index")
    public String index() {
        return "index";
    }

    /*---------------------------*/
    /*书管理*/
    @GetMapping("/book")
    public String book() {
        return "book";
    }


    /*---------------------------*/
    /*修改密码*/
    @GetMapping("/changePassword")
    public String changePassword() {
        return "changePassword";
    }


}
